/**
 * 
 */
package ar.edu.unq.svnpermission.ui.model;

import ar.edu.unq.svnpermission.domain.User;
import ar.edu.unq.svnpermission.ui.exceptions.UserAlreadyExist;
import ar.unq.edu.svnpermission.homes.UserHome;
import ar.unq.edu.svnpermission.utils.Encriptor;


/**
 * Modelo para la creacion de un nuevo usuario
 *@author nicolas
 */
public class NewUser extends UseCase {
	private static final long serialVersionUID = 1L;
	private User user = new User();
	
	/**
	 * Persiste el usuario
	 * @author nicolas
	 */
	public void create()
	{
		//Si el usuario existe, lanzo una excepción
		if(this.existUser())
			throw new UserAlreadyExist();
		
		this.getHome(User.class,UserHome.class).create(this.getUser());
		
	}
	
	/**
	 * Determina si un usuario ya existe en el repositorio
	 * @return True si existe. Falso en el caso contrario
	 * @author nicolas
	 */
	private Boolean existUser()
	{
		return !this.getHome(User.class,UserHome.class).searchByExample(this.getUser()).isEmpty();
	}

	protected User getUser() {
		return user;
	}
	protected void setUser(User user) {
		this.user = user;
	}
	
	/*ACCESORS*/
	public void setPassword(String password) 
	{
		this.getUser().setPassword(Encriptor.encrypt(password));
	}
	
	public String getPassword()
	{
		return this.getUser().getPassword();
	}
	
	public String getName() {
		return this.getUser().getName();
	}
	
	public void setName(String name) {
		this.getUser().setName(name);
	}
	
	public String getLastName() {
		return this.getUser().getLastName();
	}
	
	public void setLastName(String lastName) {
		this.getUser().setLastName(lastName);
	}
	
	public String getEmail() {
		return this.getUser().getEmail();
	}
	
	public void setEmail(String email) {
		this.getUser().setEmail(email);
	}	
	
	
	
	

	

}
